package cn.huaqingcheng.tianshu.core.sys.service;

import cn.huaqingcheng.tianshu.core.sys.adapter.FileStorageService;
import cn.huaqingcheng.tianshu.core.sys.model.SysFileScheme;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.util.List;
import java.util.Optional;

/**
 * FileStorageServiceManager
 */
@Component
@RequiredArgsConstructor
public class FileStorageServiceManager {

    private final List<FileStorageService> storageServices;


    public Optional<FileStorageService> select(String scheme) {
        return select(SysFileScheme.of(scheme));
    }

    public Optional<FileStorageService> select(SysFileScheme scheme) {
        for (FileStorageService service : storageServices) {
            if (service.support(scheme)) {
                return Optional.of(service);
            }
        }
        return Optional.empty();
    }

    public Optional<FileStorageService> select(URI uri) {
        return select(SysFileScheme.of(uri.getScheme()));
    }

}
